BUILD_TARGET = RTThread-Test
BUILD_DIR = build

C_SOURCES = \
system/drivers/platform/core_riscv.c \
system/rtthread_bsp/drivers/drv_gpio.c \
system/rtthread_bsp/drivers/drv_usart.c \
system/drivers/peripheral/src/ch32v30x_adc.c \
system/drivers/peripheral/src/ch32v30x_bkp.c \
system/drivers/peripheral/src/ch32v30x_can.c \
system/drivers/peripheral/src/ch32v30x_crc.c \
system/drivers/peripheral/src/ch32v30x_dac.c \
system/drivers/peripheral/src/ch32v30x_dbgmcu.c \
system/drivers/peripheral/src/ch32v30x_dma.c \
system/drivers/peripheral/src/ch32v30x_dvp.c \
system/drivers/peripheral/src/ch32v30x_eth.c \
system/drivers/peripheral/src/ch32v30x_exti.c \
system/drivers/peripheral/src/ch32v30x_flash.c \
system/drivers/peripheral/src/ch32v30x_fsmc.c \
system/drivers/peripheral/src/ch32v30x_gpio.c \
system/drivers/peripheral/src/ch32v30x_i2c.c \
system/drivers/peripheral/src/ch32v30x_iwdg.c \
system/drivers/peripheral/src/ch32v30x_misc.c \
system/drivers/peripheral/src/ch32v30x_opa.c \
system/drivers/peripheral/src/ch32v30x_pwr.c \
system/drivers/peripheral/src/ch32v30x_rcc.c \
system/drivers/peripheral/src/ch32v30x_rng.c \
system/drivers/peripheral/src/ch32v30x_rtc.c \
system/drivers/peripheral/src/ch32v30x_sdio.c \
system/drivers/peripheral/src/ch32v30x_spi.c \
system/drivers/peripheral/src/ch32v30x_tim.c \
system/drivers/peripheral/src/ch32v30x_usart.c \
system/drivers/peripheral/src/ch32v30x_wwdg.c \
app/main.c \
system/services.c \

CPP_SOURCES = \

ASM_SOURCES = \
startup_ch32v30x.S

INCLUDES = \
-Isystem/drivers/platform \
-Iapp \
-Isystem \
-Isystem/rtthread_bsp/drivers \
-Isystem/drivers/peripheral/inc 

LD_SCRIPT = link.ld

# 组件选项
RTT_DIR = system/rtthread
FLASH_TOOL = ch32v-flash
MONITOR_TOOL = minicom -D

ifdef RTT_DIR

C_SOURCES += \
$(RTT_DIR)/components/drivers/serial/serial.c \
$(RTT_DIR)/components/drivers/misc/pin.c \
$(RTT_DIR)/components/finsh/cmd.c \
$(RTT_DIR)/components/finsh/msh_cmd.c \
$(RTT_DIR)/components/finsh/msh_file.c \
$(RTT_DIR)/components/finsh/msh.c \
$(RTT_DIR)/components/finsh/shell.c \
$(RTT_DIR)/components/finsh/symbol.c \
$(RTT_DIR)/libcpu/risc-v/common/cpuport.c \
$(RTT_DIR)/src/clock.c \
$(RTT_DIR)/src/components.c \
$(RTT_DIR)/src/cpu.c \
$(RTT_DIR)/src/device.c \
$(RTT_DIR)/src/idle.c \
$(RTT_DIR)/src/ipc.c \
$(RTT_DIR)/src/irq.c \
$(RTT_DIR)/src/kservice.c \
$(RTT_DIR)/src/mem.c \
$(RTT_DIR)/src/memheap.c \
$(RTT_DIR)/src/mempool.c \
$(RTT_DIR)/src/object.c \
$(RTT_DIR)/src/scheduler.c \
$(RTT_DIR)/src/slab.c \
$(RTT_DIR)/src/thread.c \
$(RTT_DIR)/src/timer.c \
$(RTT_DIR)/board.c 

INCLUDES += \
-I$(RTT_DIR)/components/drivers/include \
-I$(RTT_DIR)/components/finsh \
-I$(RTT_DIR)/include \
-I$(RTT_DIR)/libcpu/risc-v/common \
-I$(RTT_DIR)

ASM_SOURCES += \
$(RTT_DIR)/libcpu/risc-v/common/context_gcc.S \
$(RTT_DIR)/libcpu/risc-v/common/interrupt_gcc.S

endif


PREFIX = riscv-none-embed-
CC = $(PREFIX)gcc
CPP = $(PREFIX)g++
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size

C_FLAGS = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wunused -Wuninitialized  -g $(INCLUDES) -std=gnu99 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -c 
CPP_FLAGS = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wunused -Wuninitialized  -g $(INCLUDES) -std=gnu99 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -c
AS_FLAGS = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wunused -Wuninitialized  -g $(INCLUDES) -std=gnu99 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -c


LIBS :=

LD_FLAGS = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wunused -Wuninitialized  -g -T $(LD_SCRIPT) -nostartfiles -Xlinker --gc-sections -Wl,-Map,"$(BUILD_DIR)/$(BUILD_TARGET).map" --specs=nano.specs --specs=nosys.specs $(LIBS)

OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CPP_SOURCES:.c=.o)))
vpath %.cpp $(sort $(dir $(CPP_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.S=.o)))
vpath %.S $(sort $(dir $(ASM_SOURCES)))

all:$(BUILD_DIR)/$(BUILD_TARGET).elf $(BUILD_DIR)/$(BUILD_TARGET).bin $(BUILD_DIR)/$(BUILD_TARGET).hex
	@echo "Build Complete"

$(BUILD_DIR)/%.o:%.c Makefile | $(BUILD_DIR)
	$(CC) -c $(C_FLAGS) $< -o $@

$(BUILD_DIR)/%.o:%.cpp Makefile | $(BUILD_DIR)
	$(CPP) -c $(CPP_FLAGS) $< -o $@

$(BUILD_DIR)/%.o:%.S Makefile | $(BUILD_DIR)
	$(AS) -c $(AS_FLAGS) $< -o $@

$(BUILD_DIR)/$(BUILD_TARGET).elf:$(OBJECTS) Makefile
	@echo $(OBJECTS)
	$(CC) $(LD_FLAGS) $(OBJECTS) -o $@
	$(SZ) $@

$(BUILD_DIR)/$(BUILD_TARGET).bin:$(BUILD_DIR)/$(BUILD_TARGET).elf | $(BUILD_DIR)
	$(CP) -O binary -S $< $@

$(BUILD_DIR)/$(BUILD_TARGET).hex:$(BUILD_DIR)/$(BUILD_TARGET).elf | $(BUILD_DIR)
	$(CP) -O ihex $< $@

$(BUILD_DIR):
	mkdir $@

.PHONY:clean
clean:
	@echo "Cleaning $(BUILD_TARGET)"
	rm -rf $(BUILD_DIR)

ifdef FLASH_TOOL

.PHONY:flash
flash:
	@echo "Downloading $(BUILD_TARGET)"
	ch32v-flash $(BUILD_DIR)/$(BUILD_TARGET).elf

endif

ifdef MONITOR_TOOL
.PHONY:monitor
monitor:
ifdef MONITOR_PORT
	$(MONITOR_TOOL) $(MONITOR_PORT)
else
	$(MONITOR_TOOL)
endif
endif